package com.sundaying.gooway.util;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class ToHtmlPath extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String url = "";
		String toUrl = "";

		ServletContext sc = getServletContext();
		// 将要访问的jsp文件路径
		String path = request.getParameter("path");
		// 将要访问的jsp文件名称：例如ToHtmlPath?filename=test.jsp
		String fileName = request.getParameter("fileName");
		// 转换后保存的文件路径
		String toPath = request.getParameter("toPath");
		// 转换后保存的文件名称
		String toFileName = request.getParameter("toFileName");

		// 获取不包含文件扩展名的新文件名
		if (toFileName == null || toFileName == "") {
			toFileName = fileName.substring(fileName.indexOf("=") + 1);
			if (toFileName.indexOf(".") > 0) {
				toFileName = fileName.substring(0, fileName.indexOf("."));
			}
		}
		
		// 获取将要访问页面的URL
		if (path == null || path == "") {
			url = "/" + fileName;
		} else {
			url = "/" + path + "/" + fileName;
		}
		
		if (toPath == null || toPath == "") {
			toPath = path;
		}
		// 下面构造要保存的文件名，及路径。
		// 1、如果有toPath，则保存在toPath下。
		// 2、如果有path则保存在path下。
		// 3、否则，保存在根目录下。
		// ConfConstants.CONTEXT_PATH为你的上下文路径。
		// 获取转换后保存页面的磁盘路径
		if (toPath == null || toPath == "") {
			toUrl = "\\" + toFileName + ".htm";									
		} else {
			toUrl = "\\" + toPath + "\\" + toFileName + ".htm";
		}

		// 访问请求的页面，并生成指定的文件。
		RequestDispatcher rd = sc.getRequestDispatcher(url);

		final ByteArrayOutputStream os = new ByteArrayOutputStream();

		final ServletOutputStream stream = new ServletOutputStream() {
			public void write(byte[] data, int offset, int length) {
				os.write(data, offset, length);
			}

			public void write(int b) throws IOException {
				os.write(b);
			}
		};

		final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));

		HttpServletResponse rep = new HttpServletResponseWrapper(response) {
			public ServletOutputStream getOutputStream() {
				return stream;
			}

			public PrintWriter getWriter() {
				return pw;
			}
		};
		
		rd.include(request, rep);
		pw.flush();
		FileOutputStream fos = new FileOutputStream(toUrl); // 把jsp输出的内容写到xxx.htm
		os.writeTo(fos);
		fos.close();
		
		PrintWriter out = response.getWriter();
		out.print("<p align=center><font size=3 color=red>success！</font></p>");
		out.println(url);
		out.println(toUrl);
		
//		response.sendRedirect(request.getContextPath() + "/" + toFileName + ".htm");
	}
}
